-- 玩家每日分享游戏奖励房卡
ALTER proc [dbo].[CPP_User_Update_ShareFinish]
  @dwUserID INT,
  @dwKindID INT,
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS
BEGIN

  DECLARE @Channel INT = 0
  SELECT @Channel = Channel FROM RYAccountsDB.dbo.AccountsInfo WHERE AccountsType = 0 AND UserID = @dwUserID
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']不存在'
    RETURN 1
  END

  -- 渠道号判断
  DECLARE @State INT = 0
  SELECT @State = State FROM RYPlatformDB.dbo.GameChannel WHERE ID = @Channel
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']的渠道[' + CAST(@Channel AS NVARCHAR) + ']不存在';
    RETURN 3
  END
  IF @State<>1 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']的渠道[' + CAST(@Channel AS NVARCHAR) + ']未启用';
    RETURN 4
  END

  -- 小游戏判断
  DECLARE @KindName NVARCHAR(255) = 'XX游戏'
  SELECT @KindName = KindName FROM RYPlatformDB.dbo.GameKindItem WHERE Nullity = 0 AND KindID = @dwKindID
  IF @@ROWCOUNT<>1 BEGIN
    SET @szResult = N'游戏[' + CAST(@dwKindID AS NVARCHAR) + ']未定义'
    RETURN 5
  END

  DECLARE @IsShare INT = 0
  DECLARE @DateID INT = CAST(CAST(GETDATE() AS FLOAT) AS INT)
  SELECT @IsShare = ID FROM [RYTreasureDB].[dbo].[RecordSharePresent] WHERE [DateID] = @DateID AND [UserID] = @dwUserID AND [KindID] = @dwKindID
  IF @IsShare>0 BEGIN
    SET @szResult = '今日分享[' + @KindName + ']已奖励'
    RETURN 6
  END

  -- 获取对应赠送房卡数
  DECLARE @ShareFinish AS INT = 0
  SELECT @ShareFinish = ZShare FROM RYPlatformDB.dbo.GameChannelKind WHERE [State] = 1 AND Channel = @Channel AND KindID = @dwKindID
  IF @@ROWCOUNT<>1 OR @ShareFinish<=0 BEGIN
    SET @szResult = N'无法确定游戏[' + @KindName + '][' + @dwKindID + ']的分享奖励房卡数'
    RETURN 7
  END

  DECLARE @dwResult INT = 0
  EXEC @dwResult = RYAccountsDB.dbo.CPP_User_RoomCard_Write @dwUserID, @ShareFinish, 7, @dwKindID
  IF @dwResult<>0 BEGIN
    SET @szResult = N'今日分享[' + @KindName + ']奖励失败!'
    RETURN 8
  END

  INSERT INTO [RYTreasureDB].[dbo].[RecordSharePresent] (
    [DateID], [UserID], [KindID], [Present]
  ) VALUES (
    @DateID, @dwUserID, @dwKindID, @ShareFinish
  )
  
  SET @szResult = N'今日分享[' + @KindName + ']奖励完成!'
  RETURN 0
END
